package com.example.demo_dongruanD.controller;

import com.example.demo_dongruanD.dto.OrderDetailRequestDto;
import com.example.demo_dongruanD.dto.OrderResponseDto;
import com.example.demo_dongruanD.dto.OrderStateDto;
import com.example.demo_dongruanD.dto.RequestDto;
import com.example.demo_dongruanD.response.ApiResponse;
import com.example.demo_dongruanD.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 医生查询用户套餐订单列表
     * @param requestDto
     * @return
     */
    @PostMapping("/list")
    public ApiResponse<OrderResponseDto> listOrders(@RequestBody RequestDto requestDto) {
        ApiResponse<OrderResponseDto> response = ordersService.listOrders(requestDto);
        return new ApiResponse<>(response.getCode(), response.getMsg(), response.getData(), response.getTotal());
    }

    /**
     * 医生查询精准用户套餐订单信息
      * @param requestDto
     * @return
     */
    @PostMapping(value = "/listdetail", consumes = "application/json")
    public ApiResponse<OrderResponseDto> listOrdersDetail(@RequestBody OrderDetailRequestDto requestDto) {
        OrderResponseDto orderResponseDto = ordersService.listOrdersDetail(requestDto);
        return new ApiResponse<>(200, "Success", orderResponseDto, orderResponseDto.getTotal());
    }


    /**
    * 用于订单归档
    * */
    @PostMapping("/statechange")
    public ResponseEntity<ApiResponse<Void>> changeOrderState(@RequestBody OrderStateDto orderIdDto) {
        // Validate the orderIdDto if necessary
        if (orderIdDto == null || orderIdDto.getOrderId() == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(new ApiResponse<>(400, "orderId is required"));
        }

        // Update the order state
        ordersService.updateOrderState(orderIdDto.getOrderId());

        // Prepare the response
        ApiResponse<Void> response = new ApiResponse<>(200, "Order state updated successfully");
        return ResponseEntity.ok(response);
    }
}
